package com.night.icm.controller.base;

import com.night.icm.common.utils.PageDataDTO;
import com.night.icm.common.utils.SimpleDataDTO;
import com.night.icm.model.base.StudentDTO;
import com.night.icm.service.base.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;

/**
 * StudentController
 *
 * @author Liao ZhiYong
 * @date 2019/12/21
 */
@Api(tags = {"学生信息管理"})
@RestController
@RequestMapping("/api/icm-student")
public class StudentController {

    private final StudentService studentService;

    @Autowired
    public StudentController(StudentService studentService) {
        this.studentService = studentService;
    }


    @ApiOperation(value = "分页查询学生信息")
    @GetMapping(value = "")
    public PageDataDTO<StudentDTO> findOnePage(
            @ApiParam(value = "页号，从0开始", required = true, defaultValue = "0") @RequestParam("page") @Min(0) int page,
            @ApiParam(value = "每页记录条数", required = true, defaultValue = "30") @RequestParam("size") @Min(1) @Max(100) int size,
            @ApiParam(value = "排序字段，例如：字段1,asc,字段2,desc") @RequestParam(value = "sort", required = false, defaultValue = "id,desc") String sort,
            @ApiParam(value = "学生学号") @RequestParam(value = "code", required = false) String code,
            @ApiParam(value = "学生姓名") @RequestParam(value = "name", required = false) String name,
            @ApiParam(value = "年级，即入学年份") @RequestParam(value = "grade", required = false) String grade,
            @ApiParam(value = "所属学院") @RequestParam(value = "academyId", required = false) String academyId,
            @ApiParam(value = "所属专业") @RequestParam(value = "professionId", required = false) String professionId,
            @ApiParam(value = "所属班级") @RequestParam(value = "classId", required = false) String classId
    ) {
        return this.studentService.findOnePage(page, size, sort, code, name, grade, academyId, professionId, classId);
    }

    @ApiOperation(value = "新增学生信息")
    @PostMapping(value = "")
    public StudentDTO create(
            @ApiParam(value = "学生信息", required = true) @RequestBody @Validated StudentDTO studentDTO
    ) {
        return this.studentService.create(studentDTO);
    }

    @ApiOperation(value = "根据id删除学生")
    @DeleteMapping(value = "/{id}")
    public void delete(@ApiParam(value = "学生id", required = true) @PathVariable(name = "id") String id) {
        this.studentService.delete(id);
    }

    @ApiOperation(value = "更新学生信息")
    @PutMapping(value = "/{id}")
    public StudentDTO update(
            @ApiParam(value = "学生id", required = true) @PathVariable(name = "id") String id,
            @ApiParam(value = "学生信息", required = true) @RequestBody @Validated StudentDTO studentDTO
    ) {
        return this.studentService.update(id, studentDTO);
    }

    @ApiOperation(value = "根据id查找学生")
    @GetMapping(value = "/{id}")
    public StudentDTO findById(@ApiParam(value = "学生id", required = true) @PathVariable(name = "id") String id) {
        return this.studentService.findById(id);
    }

    @ApiOperation(value = "查找所有学生信息，可选择排序")
    @GetMapping(value = "/all")
    public List<StudentDTO> findAll(
            @ApiParam(value = "排序参数") @RequestParam(value = "param", required = false, defaultValue = "id") String param,
            @ApiParam(value = "排序模式: asc/desc") @RequestParam(value = "pattern", required = false, defaultValue = "asc") String pattern
    ) {
        return this.studentService.findAll(param, pattern);
    }

    @ApiOperation(value = "检查学生学号是否已存在")
    @GetMapping(value = "/checkCodeExist")
    public SimpleDataDTO<Boolean> checkCodeExist(@ApiParam(value = "学生code", required = true) @RequestParam(value = "code") String code) {
        return this.studentService.checkCodeExist(code);
    }

    @ApiOperation(value = "根据用户id查找学生")
    @GetMapping(value = "/byUser/{userId}")
    public StudentDTO findByUserId(@ApiParam(value = "用户id", required = true) @PathVariable(name = "userId") String userId) {
        return this.studentService.findByUserId(userId);
    }

    @ApiOperation(value = "统计学生数量")
    @GetMapping(value = "/count")
    public Long countStudent() {
        return this.studentService.countStudent();
    }
}